package com.way.service.impl;

import com.way.domain.dto.SysUserDto;
import com.way.domain.entity.SysUser;
import com.way.domain.vo.SysUserVo;
import com.way.repository.SysUserRepository;
import com.way.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserRepository repository;

    @Override
    public List<SysUserVo> findAll() {
        List<SysUser> all = repository.findAll();
        List<SysUserVo> vos = new ArrayList<>();
        for (SysUser sysUser : all) {
            SysUserVo vo = new SysUserVo();
            BeanUtils.copyProperties(sysUser, vo);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public SysUserVo findById(Integer id) {
        Optional<SysUser> sysUser = repository.findById(id);
        SysUserVo vo = new SysUserVo();
        sysUser.ifPresent(user -> BeanUtils.copyProperties(user, vo));
        return vo;
    }

    @Override
    public void save(SysUserDto dto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(dto, sysUser);
        sysUser.setFlag(1);
        sysUser.setIsDeleted(0);
        repository.save(sysUser);
    }

    @Override
    public void delete(Integer id) {
        repository.deleteById(id);
    }

}
